package com.polaris.live.adapter.live

import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.common.adapter.BaseAdapter
import com.polaris.live.common.ext.autoDp
import com.polaris.live.constant.LiveConst
import com.polaris.live.databinding.AdapterHomeLiveList2Binding
import com.polaris.live.databinding.AdapterListBannerBinding
import com.polaris.live.popup.GlobalPopupManager
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.resp.back_resp.ListWithBannerBean
import com.polaris.live.utils.CommonUtils.toBoolean
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.image.loadWejoyImage
import com.polaris.live.utils.route.RouterUtils
import com.polaris.live.widget.refresh.BaseBindingViewHolder

/**
 * HomePartyAdapter
 *
 * @author Created by 半仙 on 2024/3/5/005 11:46
 */
class HomePartyAdapter : BaseAdapter<Any, BaseViewHolder>(0) {

    override fun getDefItemViewType(position: Int): Int {
        return when (val item = data[position]) {
            is DisplayUserBean -> TYPE_NORMAL
            is ListWithBannerBean -> TYPE_BANNER
            else -> throw IllegalArgumentException("Unknown item type: $item")
        }
    }

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): BaseBindingViewHolder<*, *> {
        val layoutInflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            TYPE_BANNER -> BannerViewHolder(
                AdapterListBannerBinding.inflate(layoutInflater, parent, false)
            )

            TYPE_NORMAL -> NormalManViewHolder(
                AdapterHomeLiveList2Binding.inflate(layoutInflater, parent, false)
            )

            else -> throw IllegalArgumentException("Invalid view type: $viewType")
        }
    }

    override fun convert(holder: BaseViewHolder, item: Any) {
        when (holder) {
            is NormalManViewHolder -> if (item is DisplayUserBean) holder.bind(item)
            is BannerViewHolder -> if (item is ListWithBannerBean) holder.bind(item)
        }
    }

    class NormalManViewHolder(
        binding: AdapterHomeLiveList2Binding,
    ) : BaseBindingViewHolder<AdapterHomeLiveList2Binding, DisplayUserBean>(binding) {

        override fun bind(item: DisplayUserBean) {
            mBinding.apply {
                Glide.with(mBinding.root.context)
                    .loadWejoyImage(item.cover)
                    .transform(CenterCrop(), RoundedCorners(8.autoDp))
                    .into(userCover)

                nickName.text = item.base.nickname

                if (TextUtils.isEmpty(item.base.country)) {
                    userCountry.visibility = View.GONE
                } else {
                    userCountry.visibility = View.VISIBLE
                    userCountry.setCountry(item.base.country)
                }

                svState.setState(item.base, item)
                tvUserCount.text = item.stayUser.toString()
                headList.setList(item.siteUsers)

                val user = item.base
                val partyGameType = user.partyGameType
                val showGameImg =
                    user.living?.toBoolean() == true && user.liveType == LiveConst.Type.PARTY_GAME_LIVE && partyGameType != null
                if (showGameImg) {
                    Glide.with(ivGameType).load(ImageUtils.imgPath(item.gameIcon)).into(ivGameType)
                    ivGameType.visibility = View.VISIBLE
                } else {
                    ivGameType.visibility = View.GONE
                }
                widgetSubscript.setUserSymbol(item.symbol)
            }
        }
    }

    companion object {

        const val TYPE_BANNER = 0
        const val TYPE_NORMAL = 1
    }
}

class BannerViewHolder(binding: AdapterListBannerBinding, roundCorner: Int? = null) :
    BaseBindingViewHolder<AdapterListBannerBinding, ListWithBannerBean>(binding) {

    private val mRoundCorner = roundCorner

    override fun bind(item: ListWithBannerBean) {

        val adapter = HomeBannerAbsAdapter()
        mBinding.homeAbsBanner.apply {
            setAdapter(adapter)
            setIndicatorVisibility(View.GONE)
            setInterval(5000)
            if (mRoundCorner != null) {
                setRoundCorner(mRoundCorner.autoDp)
            }
            setOnPageClickListener { _, position ->
                GlobalPopupManager.showCompletePopup(context, isOk = {
                    val itemData = item.banners.getOrNull(position) ?: return@showCompletePopup
                    val link = itemData.link.takeIf { it?.isNotEmpty() == true }
                        ?: itemData.nativeLink.takeIf { it?.isNotEmpty() == true }
                    link?.let { RouterUtils.routerGoto(it) }
                })
            }
            create(item.banners)
        }
    }
}